package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Slf4j
@Service
public class DishServiceImpI implements DishService {
    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    private final DishMapper dishMapper;
    private final DishFlavorMapper dishFlavorMapper;
    private final RedisTemplate redisTemplate;

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        List<Dish> dishList = dishMapper.pageQuery(dishPageQueryDTO);
        Page<Dish> page = (Page<Dish>) dishList;
        long total = page.getTotal();
        List<Dish> records = page.getResult();
        return new PageResult(total, records);
    }

    /**
     * 新增菜品和对应的口味
     *
     * @param dishDTO
     * @return
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        //向菜品表插入一条数据
        dishMapper.insert(dish);

        //获取insert语句生成的主键值
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表插入数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    public void deleteDishBatch(List<Long> ids) {
        dishMapper.deleteDishBatch(ids);
    }


    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        if (dishDTO.getId() == null) {
            throw new RuntimeException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //1、更新菜品基本信息
        dishMapper.updateDish(dish);
        //2、更新口味儿表
        Long dishId = dish.getId();
        // 2.1 删除该菜品所有原有口味
        dishFlavorMapper.deleteByDishId(dishId);
        //2.2插入新的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() > 0 && flavors != null) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertBatch(flavors);
        }


    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    public DishVO queryById(Long dishId) {
        if (dishId == null) {
            throw new RuntimeException("菜品id为空");
        }

        // 查询菜品基本信息
        DishVO dishVO = dishMapper.getDishById(dishId);
        if (dishVO == null) {
            throw new RuntimeException("菜品不存在，id: " + dishId);
        }

        // 查询菜品口味（如果需要）
        List<DishFlavor> flavors = dishMapper.getFlavorsByDishId(dishId);
        dishVO.setFlavors(flavors);
        return dishVO;

    }

    /**
     * 菜品起售、停售
     *
     * @param status
     */
    @Override
    public void StartOrStop(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDish(dish);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> queryDishByCategoryId(Long categoryId) {

        List<Dish> dishList = dishMapper.queryDishByCategoryId(categoryId);
        return dishList;
    }


    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {

        List<Dish> dishList = dishMapper.queryDishByCategoryId(dish.getCategoryId());
        List<DishVO> dishVOList = new ArrayList<>();
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d, dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
